﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Road3App.Model.RoadNs;

namespace Road3App.Views.UserControls
{
    /// <summary>
    /// Interaction logic for RoadUC.xaml
    /// </summary>
    public partial class RoadUC : TransformControl
    {
        public RoadUC()
        {
            InitializeComponent();
        }

        public RoadUC(Road road)
            : this()
        {
            this.DataContext = road;

            road.ObjectAdded += (s, ea) =>
                {
                    var rv = ea.Obj as RoadVehicle;
                    if (rv == null)
                        return;
                    RoadVehicleUC rvuc = new RoadVehicleUC(rv);
                    LayoutRoot.Children.Add(rvuc);
                };

            road.ObjectRemoved += (s, ea) =>
                {
                    var rv = ea.Obj as RoadVehicle;
                    if (rv == null)
                        return;
                    var rvuc = 
                        LayoutRoot.Children
                        .OfType<RoadVehicleUC>()
                        .Where(r => (r.DataContext as RoadVehicle) == rv)
                        .FirstOrDefault();
                    if (rvuc == null)
                        return;
                    LayoutRoot.Children.Remove(rvuc);
                };
        }
    }
}
